<?php
class Control_DataAnalysis_getLossLevelNum {

	function getLossLevelNum() {
		$nowGame = $game 	= MooForm::request('game');
		$action 	= MooForm::request('action');
		$viewDate = date("Y-m-d",strtotime("-2 day"));
		//未操作
		if($action == 0) {
			MooView::set('registerDate', $viewDate);
			MooView::render('getLossLevelNum');
		}
		
		$platform 	= MooForm::request('platform');
		$nowServer 	= MooForm::request('server');
		$type 		= MooForm::request('type');
		$registerDate	= MooForm::request('registerDate'); //注册时间
		
		if($registerDate) {
			MooView::set('registerDate', $registerDate);
		}
		
		/*$serArr = explode("-", $nowServer);
		$minServer = $serArr[0];
		$maxServer = $serArr[1];*/
		
		$game = MooObj::get('Game')->transportGame($game);
		
		$platformInfo = MooObj::get('Game')->getPlatformInfoByGame($game);
		if($platformInfo) {
			MooView::set('platformList', $platformInfo);
		}

		MooObj::get('Control_Data')->setPlatformData($game);

        //有查询条件 获取区服信息
        $servers = MooObj::get('Gm')->getServerListByGame($game);
        MooView::set('servers', $servers);

		$serverLists = MooObj::get('Gm')->getServerListByGame($game);
		/*if($platform) {
			foreach ($serverLists as $key => $val) {
				if($val['platform'] != $platform) {
					unset($serverLists[$key]);
				}
				if($val['platform'] == 'test') {
					unset($serverLists[$key]);
				}
			}
		}*/
		
		// 后台用户名
		$userName = MooObj::get('User')->getUserName();
		// $params['user'] = $userName;
		
		$falseData = 0;
		$levelSession = MooConfig::get('levelsession.lossLevel');
		$bingData = array();
		$serverStatusDatas = array();
		$time1 = time();
		$getLevelUrl = MooConfig::get('main.dataAnalysis.fetch_loss');
		$id = 0;



        $url = $getLevelUrl."?game={$nowGame}&platform={$platform}&server={$nowServer}&timeForReg=".strtotime($registerDate);
        $res = MooUtil::curl_send($url, null, 1);
        $returnData = MooJson::decode($res, true);
        $returnData = array('serverName'=>'test-1','total'=>100,'detail'=>array('L1-L5'=>'5','L6-L10'=>'2','L11-L15'=>'8','L16-L20'=>'3','L21-L25'=>'7','L26-L30'=>'5','L31-L35'=>'1','L36-L40'=>'9','L41-L45'=>'3','L46-L50'=>'7','L51-L55'=>'4','L56-L60'=>'6','L61-L65'=>'5','L66-L70'=>'5','L71-L75'=>'0','L76-L80'=>'10','L81-L85'=>'6','L86-L90'=>'4','L91-L95'=>'1','L96-L100'=>'9'));
        if($res  && $returnData){
            $serverStatusDatas['server'] = $returnData['serverName'];
            foreach ($returnData['detail'] as $key => $val){
                switch ($key){
                    case "L1-L5":
                        $curKey = 'L1';
                         break;
                    case "L6-L10":
                        $curKey = 'L2';
                        break;
                    case "L11-L15":
                        $curKey = 'L3';
                        break;
                    case "L16-L20":
                        $curKey = 'L4';
                        break;
                    case "L21-L25":
                        $curKey = 'L5';
                        break;
                    case "L26-L30":
                        $curKey = 'L6';
                        break;
                    case "L31-L35":
                        $curKey = 'L7';
                        break;
                    case "L36-L40":
                        $curKey = 'L8';
                        break;
                    case "L41-L45":
                        $curKey = 'L9';
                        break;
                    case "L46-L50":
                        $curKey = 'L10';
                        break;
                    case "L51-L55":
                        $curKey = 'L11';
                        break;
                    case "L56-L60":
                        $curKey = 'L12';
                        break;
                    case "L61-L65":
                        $curKey = 'L13';
                        break;
                    case "L66-L70":
                        $curKey = 'L14';
                        break;
                    case "L71-L75":
                        $curKey = 'L15';
                        break;
                    case "L76-L80":
                        $curKey = 'L16';
                        break;
                    case "L81-L85":
                        $curKey = 'L17';
                        break;
                    case "L86-L90":
                        $curKey = 'L18';
                        break;
                    case "L91-L95":
                        $curKey = 'L19';
                        break;
                    case "L96-L100":
                        $curKey = 'L20';
                        break;
                }
                $tmpNum = floor(($val/$returnData['total'])*10000)/10000*100;
                $serverStatusDatas[$curKey] =  $tmpNum."%";
            }
        }else{
            $serverStatusDatas = array('server'=>'','L1'=>'-','L2'=>'-','L3'=>'-','L4'=>'-','L5'=>'-','L6'=>'-','L7'=>'-','L8'=>'-','L9'=>'-','L10'=>'-','L11'=>'-','L12'=>'-','L13'=>'-','L14'=>'-','L15'=>'-','L16'=>'-','L17'=>'-','L18'=>'-','L19'=>'-','L20'=>'-');
        }
		$time2 = time() - $time1;
		$viewData = MooJson::encode($bingData);
		// 格式化数据
		$viewData = str_replace(',"',',',$viewData);
		$viewData = str_replace('"]',']',$viewData);
		
		if($serverStatusDatas && $type == "daochu") {
			$excelData = array();
			$name = "levelSection";
			$excelRs = array(
				0 => array(
					0 => '',
					1 => $name,
				),
				1 => array(
					'所在服',
					'level_1-5',
					'level_6-10',
					'level_11-20',
					'level_21-25',
					'level_26-30',
					'level_31-35',
					'level_36-40',
					'level_41-45',
					'level_46-50',
					'level_51-55',
					'level_56-60',
					'level_61-65',
					'level_66-70',
					'level_71-75',
					'level_76-80',
					'level_81-85',
					'level_86-90',
					'level_91-95',
					'level_96-100',
			));
			
			foreach ($serverStatusDatas as $key=>$val) {
				$arr[] = $val['server'];
				$arr[] = $val['L1'];
				$arr[] = $val['L2'];
				$arr[] = $val['L3'];
				$arr[] = $val['L4'];
				$arr[] = $val['L5'];
				$arr[] = $val['L6'];
				$arr[] = $val['L7'];
				$arr[] = $val['L8'];
				$arr[] = $val['L9'];
				$arr[] = $val['L10'];
				$arr[] = $val['L11'];
				$arr[] = $val['L12'];
				$arr[] = $val['L13'];
				$arr[] = $val['L14'];
				$arr[] = $val['L15'];
				$arr[] = $val['L16'];
				$arr[] = $val['L17'];
				$arr[] = $val['L18'];
				$arr[] = $val['L19'];
				$arr[] = $val['L20'];
				$excelData[] = $arr;
				$arr = array();
			}
			
			$excelData = array_merge($excelRs, $excelData);
			$writeExcelArr[0]['name'] = $name;
			$writeExcelArr[0]['data'] = $excelData;

			$excelPath = ROOT_PATH . "/export/orders/" . $name . ".xls";
			$rs = MooObj::get('Control_ExcelConfMaker_Excel')->write($excelPath, $writeExcelArr);
			$this->downFile($excelPath, $name . ".xls");
			exit;
		}
		
		$pageNameArr = MooObj::get('Game')->getServersBy($game, $platform);
		MooView::set('pageNameArr', $pageNameArr);
		
		//MooView::set('viewData', $viewData);
		
		MooView::set('data', $serverStatusDatas);
		MooView::set('nowServer', $nowServer);
		MooView::set('server', $nowServer);
		MooView::set('nowGame', $nowGame);
		MooView::set('costTime', $time2);
		MooView::set('platform', $platform);

		MooView::render('getLossLevelNum');
	}
		
	// 下载文件
	private function downFile($filepath, $fileName) {
		$fp			=	fopen($filepath,"r");
		$file_size	=	filesize($filepath);
		//下载文件需要用到的头
		header("Content-type: application/octet-stream");
		header("Accept-Ranges: bytes");
		header("Accept-Length:".$file_size);
		header("Content-Disposition: attachment; filename=".$fileName);
		$buffer		=	1024;
		$file_count	=	0;
		//向浏览器返回数据
		while(!feof($fp) && $file_count < $file_size){
			$file_con	=	fread($fp,$buffer);
			$file_count	+=	$buffer;
			echo $file_con;
		}
		fclose($fp);
	}
}
